import {createApp} from 'vue'
import App from './AppView.vue'
import router from './router'
import axios, {AxiosResponse} from "axios";
import DictionaryModel from "@/models/DictionaryModel";
import ElementPlus from 'element-plus';
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'

import '@/assets/custom.css'

import Vant from 'vant';	//引入vant库
import 'vant/lib/index.css';	//引入vant样式

import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import BomModel from "@/models/BomModel";
import MaterialModel from "@/models/MaterialModel";

import VForm3 from 'vform3-mobile-builds'  //引入VForm库
import 'vform3-mobile-builds/dist/designer.style.css' //引入VForm3样式
import CustomerModel from "@/models/CustomerModel";
import {getAllDict} from "@/apis/dictionary.api";

const app = createApp(App)


const globalDict = new Map<string, DictionaryModel [] | { [key: string]: string }>();
const userDict = new Map<string, MaterialModel[] | CustomerModel[] | BomModel[] | { [key: string]: string }>();

app.provide("globalDict", globalDict);
app.provide("userDict", userDict);

const initDict = async () => {
    (await getAllDict()).forEach((dict: DictionaryModel) => {
        const dicts: DictionaryModel[] | { [key: string]: string } = globalDict.get(dict.purpose) ?? []
        const dictsFlat: DictionaryModel[] | { [key: string]: string } = globalDict.get(dict.purpose + "_FLAT") ?? {}
        if (Array.isArray(dicts)) {
            dicts.push(dict);
        }
        if (!Array.isArray(dictsFlat)) {
            dictsFlat[dict.code] = dict.label;
        }
        globalDict.set(dict.purpose, dicts);
        globalDict.set(dict.purpose + "_FLAT", dictsFlat);
    })

    await axios({method: 'get', url: `/api/mat/dict`}).then((value: AxiosResponse<MaterialModel[]>) => {
        userDict.set('MATERIAL_TYPE', value.data.reduce((prev: MaterialModel[], mat: MaterialModel) => {
            prev.push(mat);
            return prev;
        }, []));
        globalDict.set('MATERIAL_TYPE_FLAT', value.data.reduce((prev: { [key: string]: string }, mat: MaterialModel) => {
            prev[mat.id] = mat.name;
            return prev;
        }, {}));
    });
    await axios({method: 'get', url: `/api/customer/dict`}).then((value: AxiosResponse<CustomerModel[]>) => {
        userDict.set('CUSTOMER_LIST', value.data.reduce((prev: CustomerModel[], customer: CustomerModel) => {
            prev.push(customer);
            return prev;
        }, []));
        userDict.set('CUSTOMER_LIST_FLAT', value.data.reduce((prev: { [key: string]: string }, customer: CustomerModel) => {
            prev[customer.id] = customer.name;
            return prev;
        }, {}));
    });
    await axios({method: 'get', url: `/api/bom/dict`}).then((value: AxiosResponse<BomModel[]>) => {
        userDict.set('BOM_LIST', value.data.reduce((prev: BomModel[], bom: BomModel) => {
            prev.push(bom);
            return prev;
        }, []));
        userDict.set('BOM_LIST_FLAT', value.data.reduce((prev: { [key: string]: string }, bom: BomModel) => {
            prev[bom.id] = bom.name;
            return prev;
        }, {}));
    });
}
await initDict()

// axios.defaults.baseURL = 'http://192.168.0.108:8098/'
// 全局挂载 axios
// app.config.globalProperties.$http = axios

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.use(ElementPlus, {locale: zhCn, size: 'small', zIndex: 3000}).use(router).mount('#app');
app.use(Vant);
app.use(VForm3);

